""""
顺丰快递
"""

import datetime
import requests
import sys
if sys.version > '3':
    PY3 = True
else:
    PY3 = False

if PY3:
    from urllib.parse import quote
else:
    from urllib import quote

def calc_price(src_code, dest_code, weight_kg_amount):
    now = datetime.datetime.now()
    out_dt = now + datetime.timedelta(hours=1)
    out_time = out_dt.strftime('%Y-%m-%dT%H:00:00+08:00')
    url = 'http://www.sf-express.com/sf-service-owf-web/service/rate/newRates?origin={src_code}&dest={dest_code}&parentOrigin={src_code}&parentDest={dest_code}&weight={weight_kg_amount}&time={out_time}&volume=0&queryType=2&lang=sc&region=cn&translate='.format(
        src_code=src_code, dest_code=dest_code, weight_kg_amount=weight_kg_amount, out_time=quote(out_time)
    )
    print('SF-URL:', url)
    r = requests.get(url)
    if r.status_code != 200:
        return False, ['网络错误', r.text, r.status_code, url]
    items = r.json()
    if len(items) == 0:
        return False, ['数据错误', items, r.status_code, url]
    return True, max([e['totalFreight'] for e in items])
